package com.service.ningdong.impl;

import com.dao.*;
import com.entity.*;
import com.service.ningdong.SynthesizeQueryService;
import org.bouncycastle.asn1.dvcs.ServiceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SynthesizeQueryServiceImpl implements SynthesizeQueryService {



    @Autowired
    private RoomstatusDao roomstatusDao;

    @Autowired
    private BuildingDao buildingDao;
    @Autowired
    private ClientDao clientDao;
    @Autowired
    private ServiceDao serviceDao;
    @Autowired
    private ServicetypeDao servicetypeDao;
    @Autowired
    private OwnershipDao ownershipDao;
    @Autowired
    private ServicestatusDao servicestatusDao;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private RoomtypeDao roomtypeDao;
    @Autowired
    private ServiceshareDao serviceshareDao;
    @Autowired
    private SharetypeDao sharetypeDao;
    @Autowired
    private ExistmortgageDao existmortgageDao;
    @Autowired
    private CloseregistrationDao closeregistrationDao;
    @Autowired
    private CloseofficeDao closeofficeDao;
    @Autowired
    private DissentingregistrationDao dissentingregistrationDao;
    @Autowired
    private UserinfoDao userinfoDao;



    //所有权查询
    @Override
    public List<OwnershipQuery> ownership(String clientname, String idcard) {
        List<OwnershipQuery> ownershipQueryList = new ArrayList<>();
        ClientExample clientExample = new ClientExample();
        if(clientname.trim()!=""){
            clientExample.createCriteria().andClientnameLike(clientname);
        }
        clientExample.createCriteria().andIdcardLike(idcard);
        List<Client> clients = clientDao.selectByExample(clientExample);
        Client client = clients.get(0);

        ServiceExample serviceExample = new ServiceExample();
        ServiceExample.Criteria criteria = serviceExample.createCriteria();
        criteria.andProposerEqualTo(client.getClientid());

        ServicetypeExample servicetypeExample = new ServicetypeExample();
        servicetypeExample.createCriteria().andServicetypeEqualTo("所有权登记");
        List<Servicetype> servicetypes = servicetypeDao.selectByExample(servicetypeExample);

        criteria.andServicetypeidEqualTo(servicetypes.get(0).getServicetypeid());
        List<com.entity.Service> serviceList = serviceDao.selectByExample(serviceExample);

        for (com.entity.Service service : serviceList) {
            Servicetype servicetype = servicetypeDao.selectByPrimaryKey(service.getServicetypeid());
            Servicestatus servicestatus = servicestatusDao.selectByPrimaryKey(service.getServicestatusid());

            Integer serviceid = service.getServiceid();
            String servicenumber = service.getServicenumber();

            //根据业务号找到共有人的信息
            ServiceshareExample serviceshareExample = new ServiceshareExample();
            serviceshareExample.createCriteria().andServiceidEqualTo(serviceid);
            List<Serviceshare> serviceshareList = serviceshareDao.selectByExample(serviceshareExample);
            List<Coowner> coownerList = new ArrayList<>();
            for(Serviceshare serviceshare:serviceshareList){
                Coowner coowner = new Coowner();
                Client client1 = clientDao.selectByPrimaryKey(serviceshare.getClientid());
                Sharetype sharetype = sharetypeDao.selectByPrimaryKey(serviceshare.getSharetypeid());
                coowner.setServiceshare(serviceshare);
                coowner.setClient(client1);
                coowner.setSharetype(sharetype);
                coownerList.add(coowner);
            }

            OwnershipExample ownershipExample = new OwnershipExample();
            ownershipExample.createCriteria().andServicenumberEqualTo(servicenumber);
            List<Ownership> ownerships = ownershipDao.selectByExample(ownershipExample);
            Ownership ownership = ownerships.get(0);

            Room room = roomDao.selectByPrimaryKey(ownership.getRoomid());
            Integer buildingid = room.getBuildingid();
            Building building = buildingDao.selectByPrimaryKey(buildingid);
            House house = houseDao.selectByPrimaryKey(building.getHid());
            Roomtype roomtype = roomtypeDao.selectByPrimaryKey(house.getRid());

            OwnershipQuery ownershipQuery = new OwnershipQuery();
            ownershipQuery.setOwnership(ownership);
            ownershipQuery.setService(service);
            ownershipQuery.setServicetype(servicetype);
            ownershipQuery.setServicestatus(servicestatus);
            ownershipQuery.setClient(client);
            ownershipQuery.setRoom(room);
            ownershipQuery.setBuilding(building);
            ownershipQuery.setHouse(house);
            ownershipQuery.setRoomtype(roomtype);
            ownershipQuery.setCoownerList(coownerList);

            ownershipQueryList.add(ownershipQuery);
        }
        return ownershipQueryList;
    }


    @Override
    public List<MortgageQuery> mortgage(String mortgager, String mortgageridcard, String mortgagee, String mortgageeidcard) {
        List<MortgageQuery> mortgageQueryList = new ArrayList<>();
        List<Existmortgage> existmortgageList = new ArrayList<>();
        //抵押人
        if(mortgageridcard.trim()!=""){
            ClientExample clientExample = new ClientExample();
            clientExample.createCriteria().andIdcardEqualTo(mortgageridcard);
            List<Client> clients = clientDao.selectByExample(clientExample);
            Client pawnor = clients.get(0);

            ExistmortgageExample existmortgageExample = new ExistmortgageExample();
            existmortgageExample.createCriteria().andMortgagerEqualTo(pawnor.getClientid());
            List<Existmortgage> existmortgageList1 = existmortgageDao.selectByExample(existmortgageExample);
            existmortgageList.addAll(existmortgageList1);
        }

        //抵押权人
        if(mortgageeidcard.trim()!=""){
            ClientExample clientExample1 = new ClientExample();
            clientExample1.createCriteria().andIdcardEqualTo(mortgageeidcard);
            List<Client> clients1 = clientDao.selectByExample(clientExample1);
            Client mortgagee2 = clients1.get(0);

            ExistmortgageExample existmortgageExample1 = new ExistmortgageExample();
            existmortgageExample1.createCriteria().andMortgagerEqualTo(mortgagee2.getClientid());
            List<Existmortgage> existmortgageList2 = existmortgageDao.selectByExample(existmortgageExample1);
            existmortgageList.addAll(existmortgageList2);

        }

        for(Existmortgage existmortgage:existmortgageList){
            MortgageQuery mortgageQuery = new MortgageQuery();
            com.entity.Service service = serviceDao.selectByPrimaryKey(existmortgage.getServiceid());
            Servicestatus servicestatus = servicestatusDao.selectByPrimaryKey(service.getServicestatusid());
            Servicetype servicetype = servicetypeDao.selectByPrimaryKey(service.getServicetypeid());
            Room room = roomDao.selectByPrimaryKey(existmortgage.getRoomid());
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            House house = houseDao.selectByPrimaryKey(building.getHid());
            Client mortgager1 = clientDao.selectByPrimaryKey(existmortgage.getMortgager());
            Client mortgagee1 = clientDao.selectByPrimaryKey(existmortgage.getMortgagee());
            mortgageQuery.setExistmortgage(existmortgage);
            mortgageQuery.setService(service);
            mortgageQuery.setRoom(room);
            mortgageQuery.setBuilding(building);
            mortgageQuery.setHouse(house);
            mortgageQuery.setMortgager(mortgager1);
            mortgageQuery.setMortgagee(mortgagee1);
            mortgageQuery.setRoomstatus(roomstatus);
            mortgageQuery.setServicestatus(servicestatus);
            mortgageQuery.setServicetype(servicetype);
            mortgageQueryList.add(mortgageQuery);
        }
        return mortgageQueryList;
    }

    /**
     * 查封信息查询
     * @param roomnumber
     * @return
     */
    @Override
    public SealQuery seal(String roomnumber) {
        SealQuery sealQuery = new SealQuery();
        CloseregistrationExample closeregistrationExample = new CloseregistrationExample();
        closeregistrationExample.createCriteria().andDescriptLike(roomnumber);
        List<Closeregistration> closeregistrations = closeregistrationDao.selectByExample(closeregistrationExample);
        Closeregistration closeregistration = closeregistrations.get(0);
        Closeoffice closeoffice = closeofficeDao.selectByPrimaryKey(closeregistration.getCloseofficeid());
        com.entity.Service service = serviceDao.selectByPrimaryKey(closeregistration.getServiceid());
        Servicetype serviceType = servicetypeDao.selectByPrimaryKey(service.getServicetypeid());
        Servicestatus servicestatus = servicestatusDao.selectByPrimaryKey(service.getServicestatusid());
        RoomExample roomExample = new RoomExample();
        roomExample.createCriteria().andRoomnumberEqualTo(roomnumber);
        List<Room> rooms = roomDao.selectByExample(roomExample);
        Room room = rooms.get(0);
        Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
        House house = houseDao.selectByPrimaryKey(building.getHid());

        sealQuery.setCloseregistration(closeregistration);
        sealQuery.setCloseoffice(closeoffice);
        sealQuery.setService(service);
        sealQuery.setServicetype(serviceType);
        sealQuery.setServicestatus(servicestatus);
        sealQuery.setRoom(room);
        sealQuery.setBuilding(building);
        sealQuery.setHouse(house);
        return sealQuery;
    }

    @Override
    public List<DiffrentQuery> dissentingregistration(String keyword) {
        List<DiffrentQuery> diffrentQueryList = new ArrayList<>();
        DissentingregistrationExample dissentingregistrationExample = new DissentingregistrationExample();
        dissentingregistrationExample.createCriteria().andDissentingitemLike("%"+keyword+"%");
        List<Dissentingregistration> dissentingregistrations = dissentingregistrationDao.selectByExample(dissentingregistrationExample);
        for(Dissentingregistration dissentingregistration:dissentingregistrations){
            Client client = clientDao.selectByPrimaryKey(dissentingregistration.getClientid());
            Userinfo userinfo = userinfoDao.selectByPrimaryKey(dissentingregistration.getUid());
            DiffrentQuery diffrentQuery = new DiffrentQuery();
            diffrentQuery.setDissentingregistration(dissentingregistration);
            diffrentQuery.setClient(client);
            diffrentQuery.setUserinfo(userinfo);
            diffrentQueryList.add(diffrentQuery);
        }
        return diffrentQueryList;
    }
}
